import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';
import { BaseEntity } from '@src/common/entity/base.entity';
import { Permission } from '@src/permission/entity/permission.entity';
import { User } from '@src/users/entity/user.entity';

@Entity({ name: 'roles' })
export class Role extends BaseEntity {
  @Column({
    type: 'varchar',
    length: 100,
    unique: true,
    comment: '角色名称，如 admin',
  })
  name: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '描述, 如超级管理员',
  })
  description?: string;

  @ManyToMany(() => Permission, (permission) => permission.roles, {
    eager: true,
    cascade: true,
  })
  @JoinTable({
    name: 'role_permission_relation',
    joinColumn: { name: 'role_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'permission_id', referencedColumnName: 'id' },
  })
  permissions: Permission[];

  @ManyToMany(() => User, (user) => user.roles)
  users: User[];
}
